//
//  JGMainViewController.m
//  TomCat_SimpleDemo
//
//  Created by mac on 15/8/13.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGMainViewController.h"

@interface JGMainViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;

//@property (strong, nonatomic) NSMutableArray *array;

@end

@implementation JGMainViewController

//-(NSMutableArray *)array
//{
//    if (!_array) {
//        self.array = [NSMutableArray array];
//        for (int i = 0; i < 13; i++) {
//            NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", @"cymbal", i];
//            
//            // ContentsOfFile需要全路径
//            NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
//            UIImage *image = [UIImage imageWithContentsOfFile:path];
//            
//            [self.array addObject:image];
//        }
//    }
//    
//    return _array;
//}

/**
 imageNamed：系统推荐使用的，但是图像实例化之后的释放由系统负责
 如果要自己释放图片，不能使用imageNamed方法！
 
 而需要使用imageWithContentsOfFile
 
 提示：如果放在Images.xcassets中的图片，不能使用imageWithContentsOfFile
 Images.xcassets中不要 存放大的，不常用的图片
 
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarHidden = YES;
    
}

//做动画方法
- (void)tomAnimationWithName:(NSString *)name count:(NSInteger)count
{
    // 如果正在动画，直接退出
    if ([self.tomImageView isAnimating]) return;
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
     //添加动画播放的图片
    for (int i = 0; i < count; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg", name, i];
        
        // ContentsOfFile需要全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        
        [array addObject:image];
    }
    
    //做动画
    self.tomImageView.animationImages = array;
    self.tomImageView.animationRepeatCount = 1;
    self.tomImageView.animationDuration = self.tomImageView.animationImages.count * 0.075;
    
    //开始动画
    [self.tomImageView startAnimating];
    
    // 动画"结束"之后，清理动画数组
    [self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];
    
    //也可以这样子清理动画
//     performSelector定义在NSObject分类中
//        [self performSelector:@selector(cleanup) withObject:nil afterDelay:self.tom.animationDuration];
}

//- (void)cleanup
//{
//    NSLog(@"%s", __func__);
////    self.tom.animationImages = nil;
//    [self.tom setAnimationImages:nil];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//打鼓
- (IBAction)cynbalsAction:(id)sender {
    [self tomAnimationWithName:@"cymbal" count:13];
}

//喝奶
- (IBAction)drinkAction:(id)sender {
     [self tomAnimationWithName:@"drink" count:81];
}

//放屁
- (IBAction)fartAction:(id)sender {
    [self tomAnimationWithName:@"fart" count:28];
}

//飞鸟
- (IBAction)larryAction:(id)sender {
    [self tomAnimationWithName:@"eat" count:40];
}

//爪子
- (IBAction)pawnAction:(id)sender {
    [self tomAnimationWithName:@"scratch" count:56];
}

//丢饼
- (IBAction)pieAction:(id)sender {
    [self tomAnimationWithName:@"pie" count:24];
}

//打头
- (IBAction)blowHeadAction:(id)sender {
    [self tomAnimationWithName:@"knockout" count:81];
}

//打肚子
- (IBAction)BlowStomachAction:(id)sender {
    [self tomAnimationWithName:@"stomach" count:34];
}

//打左脚
- (IBAction)blowLeftFoot:(id)sender {
     [self tomAnimationWithName:@"footRight" count:30];
}

//打右脚
- (IBAction)blowRightFoot:(id)sender {
     [self tomAnimationWithName:@"footLeft" count:30];
}


@end
